package com.work.servlet.teacher;

import com.work.bean.ClassW;
import com.work.service.MyClassService;
import com.work.service.WorkService;
import com.work.service.impl.MyClassServiceImpl;
import com.work.service.impl.WorkServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by Administrator on 2017/10/7.
 */
@WebServlet("/my_class")
public class MyClassServlet extends HttpServlet {

    MyClassService service = new MyClassServiceImpl();

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

        if (req.getParameter("name") != null && Integer.parseInt(req.getParameter("id")) == 0){

            service.insertClass(new ClassW(req.getParameter("name"), (Integer) req.getSession().getAttribute("teacherId")));

        }else  if (req.getParameter("name") != null && Integer.parseInt(req.getParameter("id")) != 0){

            service.update(new ClassW(Integer.parseInt(req.getParameter("id")),req.getParameter("name")));
        }

        if (req.getParameter("workID") != null){

            req.getSession().setAttribute("workID", req.getParameter("workID"));
        }else {
            req.getSession().removeAttribute("workID");
        }

        doPost(req, resp);
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {


        HttpSession session = req.getSession();

        if (session.getAttribute("isLogin") != null && (Boolean) session.getAttribute("isLogin")) {

            if (session.getAttribute("teacherId") != null){

                int countPage, whichPage;

                HashMap<Integer, List> mapClass;

                //分页
                if (req.getParameter("countPage") == null && req.getParameter("whichPage") == null) {
                    countPage = 3;
                    whichPage = 1;
                } else {
                    countPage = Integer.parseInt(req.getParameter("countPage"));
                    whichPage = Integer.parseInt(req.getParameter("whichPage"));
                }

                //删除
                if (req.getParameter("deleteId") != null) {
                    boolean flag = service.deleteById(req.getParameter("deleteId"));
                    session.setAttribute("flag", flag);
                }

                mapClass = service.pageByTeacherId(countPage,whichPage,(Integer) session.getAttribute("teacherId"));

                for (Map.Entry<Integer, List> m : mapClass.entrySet()) {

                    session.setAttribute("classes", m.getValue());
                    session.setAttribute("pageNumber", m.getKey());
                }

                session.setAttribute("countPage", countPage);
                session.setAttribute("whichPage", whichPage);

                req.getRequestDispatcher("teacher_my_class.jsp").forward(req, resp);

            }

        }else {
            session.setAttribute("error", "请登录后访问");
            resp.sendRedirect("login.jsp");
        }

    }
}
